package com.ruoyi.project.digital.service.impl;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.DateUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.project.digital.domain.PetMessage;
import com.ruoyi.project.digital.domain.TBack;
import com.ruoyi.project.digital.domain.enums.YesOrNoStatus;
import com.ruoyi.project.digital.service.IPetMessageService;
import com.ruoyi.project.digital.service.ITBackService;
import com.ruoyi.project.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.digital.mapper.UserBackMapper;
import com.ruoyi.project.digital.domain.UserBack;
import com.ruoyi.project.digital.service.IUserBackService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户背景Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-03
 */
@Service
public class UserBackServiceImpl extends ServiceImpl<UserBackMapper, UserBack> implements IUserBackService
{

    @Autowired
    private ITBackService backService;
    @Autowired
    private IPetMessageService petMessageService;
    @Autowired
    private ISysConfigService configService;
    @Autowired
    private IUserBackService userBackService;
    /**
     * 查询用户背景
     *
     * @param id 用户背景ID
     * @return 用户背景
     */
    @Override
    public UserBack selectUserBackById(Long id)
    {
        return baseMapper.selectUserBackById(id);
    }

    /**
     * 查询用户背景列表
     *
     * @param userBack 用户背景
     * @return 用户背景
     */
    @Override
    public List<UserBack> selectUserBackList(UserBack userBack)
    {
        return baseMapper.selectUserBackList(userBack);
    }

    /**
     * 新增用户背景
     *
     * @param userBack 用户背景
     * @return 结果
     */
    @Override
    public int insertUserBack(UserBack userBack)
    {
        userBack.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insertUserBack(userBack);
    }

    /**
     * 修改用户背景
     *
     * @param userBack 用户背景
     * @return 结果
     */
    @Override
    public int updateUserBack(UserBack userBack)
    {
        userBack.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateUserBack(userBack);
    }

    /**
     * 批量删除用户背景
     *
     * @param ids 需要删除的用户背景ID
     * @return 结果
     */
    @Override
    public int deleteUserBackByIds(Long[] ids)
    {
        return baseMapper.deleteUserBackByIds(ids);
    }

    /**
     * 删除用户背景信息
     *
     * @param id 用户背景ID
     * @return 结果
     */
    @Override
    public int deleteUserBackById(Long id)
    {
        return baseMapper.deleteUserBackById(id);
    }

    @Override
    public TBack selectFrontBack(Long id,Long messageId) {
        QueryWrapper<UserBack> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",id);
        queryWrapper.eq("is_deafult", YesOrNoStatus.YES.getValue());
        UserBack userBack=getOne(queryWrapper);
        if(ObjectUtil.isNull(userBack)){
            userBack=new UserBack();
            String syskey=   configService.selectConfigByKey(com.ruoyi.project.digital.domain.Constants.BACK_URL);
            TBack back=backService.getById(syskey);
            userBack.setBackUrl(back.getUrl());
            userBack.setBackId(back.getId());
            userBack.setIsDeafult(YesOrNoStatus.YES.getValue().toString());
            userBack.setUserId(id);
            userBackService.save(userBack);
        }


        TBack tBack=backService.getById(userBack.getBackId());
        PetMessage petMessage= petMessageService.getById(messageId);
        if(ObjectUtil.isNull(petMessage.getBackId())){
            petMessage.setBackId(tBack.getId());
            petMessageService.updateById(petMessage);
        }

        return tBack;
    }

    @Override
    @Transactional
    public TBack changBack(Long userId, Long messageId) {
       PetMessage petMessage= petMessageService.getById(messageId);
       Long backId=petMessage.getBackId();
       UserBack userBack=baseMapper.listRandom(userId,backId);
       if(ObjectUtil.isEmpty(userBack)){
           return null;
       }
       petMessage.setBackId(userBack.getBackId());
        petMessageService.updateById(petMessage);
        TBack tBack=backService.getById(userBack.getBackId());
        return tBack;
    }
}
